/*
	QUBE
	Sandra Obermann - QPT 1 - 2012
	fhs33730 - 1110601021
	FH Salzburg - Studiengang MultiMediaTechnolgy
*/

// Used for convertion toString() from a double to string variable
#include "cinder/Utilities.h"

// Include own written header files
#include "../include/FontDisplay.h"
#include "../include/DataManager.h"

// Used namespaces
using namespace std;

// Initialize FontDisplay properties with default values: color, font size, offset
FontDisplay::FontDisplay()
{
	m_useAlpha = true;
	m_premult = true;
	m_color = ci::ColorA( 1.0f, 1.0f, 1.0f, 1.0f );
	m_fontSizeGameOver = 0;
	m_fontSizeTime = 0;
	m_fontSizeFinish = 0;
	m_setLeadingOffsetFinish = 0;
	m_setLeadingOffsetGameOver = 0;
}

//Destructor for FontDisplay
FontDisplay::~FontDisplay()
{
	
}

// Create Font for the 3 Displays: Game Time, Game Over Screen and Finish Screen
void FontDisplay::buildFontDisplay()
{
	m_fontPositionTime = Vec2f( 25 , 25 );
	m_setLeadingOffsetGameOver = 0;
	m_setLeadingOffsetFinish = 0;

	/*
		Font positions for the following standard screen resolutions on Windows:
		1024 x 600		1024 x 768		1280 x 800		1280 x 1024
		1600 x 900		1650 x 1050		1920 x 1200
	*/

	if( ( getWindowWidth() == 1024 ) && ( getWindowHeight() == 600 ) )
	{
		m_fontSizeGameOver = 60;
		m_fontSizeTime = 40;
		m_fontSizeFinish = 60;
		m_fontPositionFinish = Vec2f( 250 , 55 );
		m_fontPositionGameOver = Vec2f( 140 , (float)getWindowHeight() / 4 + 30 );
		m_setLeadingOffsetGameOver = 10;
	}
	else if( ( getWindowWidth() == 1024 ) && ( getWindowHeight() == 768 ) )
	{
		m_fontSizeGameOver = 80;
		m_fontSizeTime = 60;
		m_fontSizeFinish = 80;
		m_fontPositionFinish = Vec2f( 170 , 60 );
		m_fontPositionGameOver = Vec2f( 60 , (float)getWindowHeight() / 4 + 50 );
		m_setLeadingOffsetGameOver = 10;
	}
	else if( ( getWindowWidth() == 1280 ) && ( getWindowHeight() == 800 ) )
	{
		m_fontSizeGameOver = 110;
		m_fontSizeTime = 80;
		m_fontSizeFinish = 100;
		m_fontPositionFinish = Vec2f( 180 , 30 );
		m_setLeadingOffsetFinish = -25;
		m_fontPositionGameOver = Vec2f( 20 , (float)getWindowHeight() / 4 + 40 );
	}
	else if( ( getWindowWidth() == 1280 ) && ( getWindowHeight() == 1024 ) )
	{
		m_fontSizeGameOver = 110;
		m_fontSizeTime = 80;
		m_fontSizeFinish = 110;
		m_fontPositionFinish = Vec2f( 130 , 90 );
		m_setLeadingOffsetFinish = -10;
		m_fontPositionGameOver = Vec2f( 20 , (float)getWindowHeight() / 4 + 60 );
		m_setLeadingOffsetGameOver = 10;
	}
	else if( ( getWindowWidth() == 1600 ) && ( getWindowHeight() == 900 ) )
	{
		m_fontSizeGameOver = 120;
		m_fontSizeTime = 100;
		m_fontSizeFinish = 100;
		m_fontPositionFinish = Vec2f( 360 , 60 );
		m_setLeadingOffsetFinish = -10;
		m_fontPositionGameOver = Vec2f( 130 , (float)getWindowHeight() / 4 + 40 );
	}
	else if( ( getWindowWidth() == 1680 ) && ( getWindowHeight() == 1050) )
	{
		m_fontSizeGameOver = 140;
		m_fontSizeTime = 100;
		m_fontSizeFinish = 120;
		m_fontPositionFinish = Vec2f( 290 , 55 );
		m_setLeadingOffsetFinish = -20;
		m_fontPositionGameOver = Vec2f( 50 , (float)getWindowHeight() / 4 + 45 );
	}
	else if( ( getWindowWidth() == 1920 ) && ( getWindowHeight() == 1200 ) )
	{
		m_fontSizeGameOver = 130;
		m_fontSizeTime = 110;
		m_fontSizeFinish = 130;
		m_fontPositionFinish = Vec2f( 340 , 55 );
		m_setLeadingOffsetFinish = -40;
		m_fontPositionGameOver = Vec2f( 30, (float)getWindowHeight() / 4 + 45 );
		m_setLeadingOffsetGameOver = 10;
	}
	else
	{
		m_fontSizeGameOver = 160;
		m_fontSizeTime = 110;
		m_fontSizeFinish = 130;
		m_fontPositionFinish = Vec2f( 340 , 75 );
		m_setLeadingOffsetFinish = -30;
		m_fontPositionGameOver = Vec2f( 50 , (float)getWindowHeight() / 4 + 15 );
	}
}

// Draw Font for the 3 Displays: Game Time, Game Over Screen and Finish Screen
void FontDisplay::drawFontDisplay( E_FONT_ID fontendID )
{
	// Text position centered over the whole window size with the font color white
	gl::setMatricesWindow( getWindowSize() );
	gl::color( Color::white() );

	// FontID for Game Over Display
	if( E_FONT_GAMEOVER == fontendID )
	{
		// Create Cinder Text layout for Game Over
		TextLayout gameOver;

		// Option for Depth Buffer Issue - on Mac every letter in 2D has the same Z-Coordinate
		gl::disableDepthRead();

		// Create Game Over Display
		gameOver.setLeadingOffset( (float)m_setLeadingOffsetGameOver );
		gameOver.setColor( m_color );
		gameOver.setFont( Font( "Cooper Black", (float)m_fontSizeGameOver ) );
		gameOver.addCenteredLine( "GAME OVER" );
		gameOver.addCenteredLine( " " );
		gameOver.addCenteredLine( "PRESS N FOR A NEW GAME");
		gameOver.addCenteredLine( "PRESS ESC TO EXIT GAME");

		// Render and draw Game Over Display
		Surface8u renderedGameOver = gameOver.render( m_useAlpha, m_premult );
		m_gameOverTexture = gl::Texture( renderedGameOver );
		gl::draw( m_gameOverTexture, m_fontPositionGameOver );

		// Enable the Depth read after drawing the Game Over Display
		gl::enableDepthRead();
	}
	// FontID for Game Time Display
	else if( E_FONT_GAMETIME == fontendID )
	{
		// Create Cinder Text layout for Game Time
		TextLayout gameTime;

		// Create String with elapsed Game Time
		stringstream m_totalTime;
		double elapsedTime = DataManager::getInstance()->getTimer()->getSessionTime();

		// Short Game Time on 2 decimal places and save into result string
		unsigned int secval = (unsigned int)( elapsedTime*10 );
		double longval = (double)( secval )/10.0f;
		m_totalTime << longval;
		// to prevent a missing zero (88) when 88.0 add it to the string
		if(longval/((unsigned int)longval)==1)
		{
			m_totalTime << ".0";
		}
		string result = m_totalTime.str();

		// Option for Depth Buffer Issue - on Mac every letter in 2D has the same Z-Coordinate
		gl::disableDepthRead();

		// Create Game Time Display
		gameTime.setColor( m_color );
		gameTime.setFont( Font( "Cooper Black", (float)m_fontSizeTime ) );
		gameTime.addLine( "QUBE TIME" );
		gameTime.addLine( result );

		// Render and draw Game Time Display
		Surface8u renderedgameTime = gameTime.render( m_useAlpha, m_premult );
		m_gameTimeTexture = gl::Texture( renderedgameTime );
		gl::draw( m_gameTimeTexture, m_fontPositionTime );

		// Enable the Depth read after drawing the Game Time Display
		gl::enableDepthRead();
	}
	// FontID for Finish Display
	else if( E_FONT_FINISHED == fontendID )
	{
		// Create Cinder Text layout for Finish
		TextLayout finish;

		// Create String with elapsed Game Time
		string finalTime = cinder::toString( DataManager::getInstance()->getTimer()->getSessionTime() );
		int pos = finalTime.find( "." );
		finalTime = finalTime.substr(0, pos + 3);

		// Option for Depth Buffer Issue - on Mac every letter in 2D has the same Z-Coordinate
		gl::disableDepthRead();

		// Create Finish Display
		finish.setColor( m_color );
		finish.setFont( Font( "Cooper Black", (float)m_fontSizeFinish ) );
		finish.addCenteredLine( "LEVEL COMPLETE" );
		finish.addCenteredLine( " " );
		finish.addCenteredLine( " " );
		finish.addCenteredLine( " " );
		finish.addCenteredLine( " " );
		finish.setLeadingOffset( (float)m_setLeadingOffsetFinish );
		finish.addCenteredLine( " " );
		finish.setFont( Font( "Cooper Black", (float)m_fontSizeFinish - 20 ) );
		finish.addCenteredLine( "YOUR QUBE TIME:  " +  finalTime );
		finish.addCenteredLine( "PRESS N FOR A NEW GAME");
		finish.addCenteredLine( "PRESS ESC TO EXIT GAME");

		// Render and draw Finish Display
		Surface8u renderedFinish = finish.render( m_useAlpha, m_premult );
		m_finishTexture = gl::Texture( renderedFinish );
		gl::draw( m_finishTexture, m_fontPositionFinish );

		// Enable the Depth read after drawing the Finish Display
		gl::enableDepthRead();
	}
}